package com.njupt.simulation.utils;

public class Antenna_1528 {
    //定义计算公式中需要的参数
    private double Gm; // 主瓣最大增益
    private double n; //天线效率
    private double LN; // 旁瓣增益衰减
    private double LF; // 前瓣增益衰减(理想值接近0dBi)
    private double psi_b; // 主瓣的半功率角



    public Antenna_1528 (double Gm, double n, double LN, double LF, double psi_b) {
        this.Gm = Gm;
        this.n = n;
        this.LF = LF;
        this.LN = LN;
        this.psi_b = psi_b;
    }

    //计算 D/λ
    private static double calculateDOverLambda(double Gm, double n) {
        return Math.sqrt(Math.pow(10 ,Gm / 10) / (n * Math.PI * Math.PI));
    }

    // 计算天线增益，输入为psi，作为分段函数的判断标准
    public  double calculateGain(double psi) {
        // 计算 D/λ
        double dOverLambda = calculateDOverLambda(Gm, n);
        if (dOverLambda < 35) {
            return calculateGainForLessThan35(psi);
        } else {
            return calculateGainForGreaterThanOrEqual35(psi);
        }
    }

    // 计算小于35的增益
    private double calculateGainForLessThan35(double psi) {
        double Y = psi_b * Math.sqrt(Math.abs(LN) / 3.0);
        double Z = Y * Math.pow(10, 0.04) * (Gm + LN - LF);
//        System.out.println(Y);
//        System.out.println(Z);

        if (0 <= psi && psi < Y) {
            return Gm - 3 * Math.pow((psi / psi_b), 2);
        } else if (Y < psi && psi < Z) {
            return Gm + LN - 25 * Math.log10(psi / Y);
        } else if (Z < psi && psi <= 180) {
            return LF;
        } else {
            throw new IllegalArgumentException("Psi value out of valid range (0-180 degrees).");
        }
    }

    // 计算大于等于35的增益
    private double calculateGainForGreaterThanOrEqual35(double psi) {
        double b =6.32;
        double X = Gm + LN + 25 * Math.log10(b * psi_b);
        double Y = b * psi_b + Math.pow(10, 0.04 * (Gm + LN - LF));
        double Z = 1 ;
        double a =0;
        double alpha = 1.5;
        double LB;  //后波瓣电平

        if((15 + LN + 0.25 * Gm + 5 * Math.log10(Z)) > 0){
            LB = 15 + LN + 0.25 * Gm + 5 * Math.log10(Z);
        }else{
            LB = 0;
        }

        switch ((int)LN) {
            case -15:
                a=2.58 * Math.sqrt(1 - 1.4 * Math.log10(Z));
                b = 6.32;
                alpha = 1.5;
                break;
            case -20:
                a=2.58 * Math.sqrt(1 -  Math.log10(Z));
                b = 6.32;
                alpha = 1.5;
                break;
            case -25:
                a=2.58 * Math.sqrt(1 - 0.6 * Math.log10(Z));
                b = 6.32;
                alpha = 1.5;
                break;
            case -30:
                a=2.58 * Math.sqrt(1 - 0.4 * Math.log10(Z));
                b = 6.32;
                alpha = 1.5;
                break;
        }

        if (psi > 0 && psi < a * psi_b) {
            return  Gm - 3 * Math.pow((psi / psi_b), alpha);
        } else if (psi >= a * psi_b && psi < 0.5 * b * psi_b) {
            return  Gm + LN + 20 * Math.log10(Z);
        } else if (psi >= 0.5 * b * psi_b && psi < b * psi_b) {
            return  Gm + LN;
        } else if (psi >= b * psi_b && psi < Y) {
            return  X - 25 * Math.log10(psi);
        } else if (psi >= Y && psi < 90) {
            return  LF;
        } else if (psi >= 90 && psi <= 180) {
            return LB;
        } else {
            throw new IllegalArgumentException("Psi value out of valid range (0-180 degrees).");
        }
    }

    public static void main(String[] args) {
        Antenna_1528 antenna = new Antenna_1528(20, 0.6, -20, -20, 3);

        for(double i=0.1;i<90;i+=0.1) {

            double gain = antenna.calculateGain(i);
            System.out.println(i);
            System.out.println(gain);
        }

    }
}

